package czhdao

import (
	"Cgo/front/models"
	"Cgo/global"
	"errors"
)

type destinationDao struct{}

var DestinationDao = destinationDao{}

// 获取对应地区目的地信息
func (d destinationDao) GetDestinationByRegion(region string) ([]models.CzhDestination, error) {
	var destinationList []models.CzhDestination
	if r := global.DB.Raw("SELECT * FROM czh_destination WHERE region LIKE CONCAT('%', ?, '%')", region).Scan(&destinationList); r.RowsAffected > 0 {
		return destinationList, nil
	} else {
		return destinationList, errors.New("没有查到数据")
	}
}

// 根据id获取对应目的地信息
func (d destinationDao) GetDestinationById(id string) (models.CzhDestination, error) {
	var destination models.CzhDestination
	if r := global.DB.Raw("SELECT * FROM czh_destination WHERE id = ?", id).Scan(&destination); r.RowsAffected > 0 {
		return destination, nil
	} else {
		return destination, errors.New("没有查到数据")
	}
}

// 添加目的地订单
func (d destinationDao) AddDestinationOrder(name string, userid string, price string, username string, people string, coverimg string) error {
	if r := global.DB.Exec("INSERT INTO czh_destination_orders (name, userid, price, username, people, coverimg) VALUES (?, ?, ?, ?, ?, ?)", name, userid, price, username, people, coverimg); r.Error != nil {
		return r.Error
	} else {
		return nil
	}
}

// 根据用户id获取对应用户订单信息
func (d destinationDao) GetDestinationOrderByUserId(userid string) ([]models.CzhDestinationOrder, error) {
	var destinationOrderList []models.CzhDestinationOrder
	if r := global.DB.Raw("SELECT * FROM czh_destination_orders WHERE userid = ?", userid).Scan(&destinationOrderList); r.RowsAffected > 0 {
		return destinationOrderList, nil
	} else {
		return destinationOrderList, errors.New("没有查到数据")
	}
}

// 获取所有目的地信息;
func (d destinationDao) GetAllDestination() ([]models.CzhDestination, error) {
	var allDestination []models.CzhDestination
	if r := global.DB.Raw("SELECT * FROM czh_destination").Scan(&allDestination); r.RowsAffected > 0 {
		return allDestination, nil
	} else {
		return allDestination, errors.New("没有查到数据")
	}
}

// 编辑目的地信息
func (d destinationDao) DestinationEdit(id int, coverimg string, name string, score string, region string, phone string, traffic string) error {
	if r := global.DB.Exec("UPDATE czh_destination SET name = ?, coverimg = ?, score = ?, region = ?, phone = ?, traffic = ? WHERE id = ?", name, coverimg, score, region, phone, traffic, id); r.Error != nil {
		return r.Error
	} else {
		return nil
	}
}

// 新增目的地信息
func (d destinationDao) DestinationAdd(coverimg string, name string, score string, region string, phone string, traffic string) error {
	if r := global.DB.Exec("INSERT INTO czh_destination (coverimg, name, score, region, phone, traffic) VALUES (?, ?, ?, ?, ?, ?)", coverimg, name, score, region, phone, traffic); r.Error != nil {
		return r.Error
	} else {
		return nil
	}
}

// 目的地删除
func (d destinationDao) DestinationRemove(id int) error {
	if r := global.DB.Exec("DELETE FROM czh_destination WHERE id = ?", id); r.Error != nil {
		return r.Error
	} else {
		return nil
	}
}
